package com.example.eos.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.eos.bean.Chain;
import com.example.eos.service.HttpService;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api")
public class ChainController {

    //获取最新节点信息
    @RequestMapping(value = "/chain/info",method = RequestMethod.GET)
    public JSONObject getInfo(){
        String info = HttpService.DoGet("http://192.168.81.130:8888/v1/chain/get_info");
        return JSON.parseObject(info);
    }

    //获取块信息
    @RequestMapping(value = "/chain/block",method = RequestMethod.POST)
    public JSONObject getBlock(@RequestBody Chain chain){
        String info = HttpService.DoPost("http://192.168.81.130:8888/v1/chain/get_block",chain);
        return JSON.parseObject(info);
    }

    //获取与帐户相关的信息
    @RequestMapping(value = "/chain/account",method = RequestMethod.POST)
    public JSONObject getAccount(@RequestBody Chain chain){
        String info = HttpService.DoPost("http://192.168.81.130:8888/v1/chain/get_account",chain);
        return JSON.parseObject(info);
    }

    //获取智能合约代码
    @RequestMapping(value = "/chain/code",method = RequestMethod.POST)
    public JSONObject getCode(@RequestBody Chain chain){
        String info = HttpService.DoPost("http://192.168.81.130:8888/v1/chain/get_account",chain);
        return JSON.parseObject(info);
    }

    //从账户获取智能合约数据
    @RequestMapping(value = "/chain/tablerows",method = RequestMethod.POST)
    public JSONObject getTableRows(@RequestBody Chain chain){
        String info = HttpService.DoPost("http://192.168.81.130:8888/v1/chain/get_table_rows",chain);
        return JSON.parseObject(info);
    }

    //将json序列化为二进制十六进制
    @RequestMapping(value = "/chain/jsontobin",method = RequestMethod.POST)
    public JSONObject jsonToBin(@RequestBody Chain chain){
        String info = HttpService.DoPost("http://192.168.81.130:8888/v1/chain/abi_json_to_bin",chain);
        return JSON.parseObject(info);
    }

    //将二进制十六进制序列化为json
    @RequestMapping(value = "/chain/bintojson",method = RequestMethod.POST)
    public JSONObject binToJson(@RequestBody Chain chain){
        String info = HttpService.DoPost("http://192.168.81.130:8888/v1/chain/abi_bin_to_json",chain);
        return JSON.parseObject(info);
    }

//    //转账
//    @RequestMapping(value = "/chain/transfor",method = RequestMethod.POST)
//    public JSONObject transfor(@RequestBody Chain chain){
//
//    }

}
